Methods for single-owner multi-consumer work queues for repeatable tasks

ABSTRACT

There are provided methods for single-owner multi-consumer work queues for repeatable tasks. A method includes permitting a single owner thread of a single owner, multi-consumer, work queue to access the work queue using atomic instructions limited to only a single access and using non-atomic operations. The method further includes restricting the single owner thread from accessing the work queue using atomic instructions involving more than one access. The method also includes synchronizing amongst other threads with respect to their respective accesses to the work queue.

CROSS-REFERENCE TO RELATED APPLICATIONS

This application is a continuation of U.S. application Ser. No.12/172,577, filed on Jul. 14, 2008, which is incorporated by referenceherein in its entirety.

GOVERNMENT RIGHTS

This invention was made with Government support under Contract No.:HR0011-07-9-0002. The Government has certain rights in this invention.

BACKGROUND

1. Technical Field

The present invention relates to information protection and queues and,more particularly, to methods for single-owner multi-consumer workqueues for repeatable tasks.

2. Description of the Related Art

Single-owner multi-consumer work queues, also commonly referred to aswork stealing queues, are typically used to hold the work created by athread, while allowing other threads to steal work if their own workqueues are empty. Since a work queue may be accessed concurrently by thequeue's owner and other threads attempting to steal work,synchronization is needed. In particular, the thread's owner is requiredto use “special” atomic instructions (e.g., compare-and-swapinstructions, also interchangeably referred to herein by the acronym“CAS”), which are typically significantly slower than regularinstructions.

In general, each task in the work queue should be extracted exactly oncefrom the queue (and hence performed exactly once, e.g., transfer money).However, in many other cases (e.g., perform a calculation), it isacceptable for tasks to be performed one or more times, i.e., when tasksare idempotent. For such latter class of tasks (i.e., those tasks to beperformed one or more times), this should be an opportunity to designwork stealing queues that guarantee correct concurrent access with lesssynchronization overheads than work stealing queues that guarantee thateach task is extracted exactly once.

SUMMARY

The shortcomings of the prior art are overcome and additional advantagesare provided through the provision of methods for lock-free workstealing queue for repeatable tasks.

According to an aspect of the present principles, there is provided amethod. The method includes permitting a single owner thread of a singleowner, multi-consumer, work queue to access the work queue using atomicinstructions limited to only a single access and using non-atomicoperations. The method further includes restricting the single ownerthread from accessing the work queue using atomic instructions involvingmore than one access. The method also includes synchronizing amongstother threads with respect to their respective accesses to the workqueue.

According to another aspect of the present principles, there is provideda program storage device readable by machine, tangibly embodying aprogram of instructions executable by the machine to perform methodsteps for queue access management. The method steps include permitting asingle owner thread of a single owner, multi-consumer, work queue toaccess the work queue using atomic instructions limited to only a singleaccess and using non-atomic operations. The method steps further includerestricting the single owner thread from accessing the work queue usingatomic instructions involving more than one access. The method stepsalso include synchronizing amongst other threads with respect to theirrespective accesses to the work queue.

These and other features and advantages will become apparent from thefollowing detailed description of illustrative embodiments thereof,which is to be read in connection with the accompanying drawings.

BRIEF DESCRIPTION OF DRAWINGS

The disclosure will provide details in the following description ofpreferred embodiments with reference to the following figures wherein:

FIG. 1 shows an exemplary work queue 100 with two tasks, to which thepresent principles may be applied, in accordance with an embodiment ofthe present principles;

FIGS. 2A, 2B, and 2C respectively show exemplary methods 200, 230, and260 relating to double-ended extraction on a work queue, in accordancewith an embodiment of the present principles;

FIG. 3 shows an exemplary work queue 300 with two tasks, to which thepresent principles may be applied, in accordance with an embodiment ofthe present principles;

FIGS. 4A, 4B, and 4C respectively show exemplary methods 400, 430, and460 relating to double-ended extraction on a work queue, in accordancewith an embodiment of the present principles;

FIGS. 5A and 5B respectively show exemplary methods 530 and 560 relatingto first in first out (FIFO) extraction on a work queue, in accordancewith an embodiment of the present principles;

FIG. 6 shows an exemplary work queue 600 with two tasks, to which thepresent principles may be applied, in accordance with an embodiment ofthe present principles; and

FIGS. 7A, 7B, and 7C respectively show exemplary methods 700, 730, and760 relating to last in first out (LIFO) extraction on a work queue, inaccordance with an embodiment of the present principles.

DETAILED DESCRIPTION OF PREFERRED EMBODIMENTS

As noted above, the present principles are directed to methods forlock-free work stealing queue for repeatable tasks.

In an embodiment, the present principles permit the queue owner toperform its operations (insertion and extraction) from the queue withoutregard for concurrent stealing operations. Thus, in an embodiment, theoperations of the queue owner do not use any complex or special atomicinstructions (as explicitly defined herein). On the other hand, thestealing threads (thieves) synchronize among each other in order tomaintain the integrity of the queue.

In an embodiment, in order to maintain the integrity of the queue evenwhen the owner is not using atomic operations, the owner never writesshared variables written by thieves (other than for initialization) andthieves never write shared variables written by the owner.

FIG. 1 shows an exemplary work queue 100 with two tasks, to which thepresent principles may be applied, in accordance with an embodiment ofthe present principles.

The reference character W represents a circular array of work items ofsize M. With respect to array W, the queue owner puts work items intothe array, and the owner and other threads may take work items from thearray. During normal queue operations (put, take, and steal), the sizeof the array is treated as constant. However, the owner of the queue canresize the array in a straightforward manner as described herein.

The reference character H denotes a single variable that can be accessedatomically. H includes three integer components corresponding to thehead of the work queue 100, the size of the work queue 100, and tag forthe work queue 100, respectively. The head of the work queue correspondsto the index of the head of the work queue, i.e., the next item to beextracted from the queue by threads other than the queue owner's thread.The size of the work queue corresponds to the number of items in thework queue 100. The tag for the work queue is a number that isincremented on every extraction. Preferably, the size of the tag islarge enough (e.g., 40 bits) such that it is impossible for the tag tomake a complete wrap-around during a single operation on the queue by athread. The initial value of H is all zeros.

FIGS. 2A, 2B, and 2C respectively show exemplary methods 200, 230, and260 relating to double-ended extraction on a work queue, in accordancewith an embodiment of the present principles. In further detail, themethod 200 of FIG. 2A corresponds to a Put(w) operation, the method 230of FIG. 2B corresponds to a Take( ) operation, and the method 260 ofFIG. 2C corresponds to a Steal( ) operation, each corresponding todouble-ended extraction on a work queue. The methods 200, 230, and 260may be applied, for example, to the work queue 100 of FIG. 1.

Initially, the Put(w) operation will be generally described, followingby a description of the method 200 of FIG. 2A. Only the owner thread(i.e., owner) of the queue can perform a Put(w) operation on the queue.The owner puts a new work item at the tail end of the work queue. ThePut(w) operation takes as a parameter the work item to be added to thequeue.

Referring to method 200 of FIG. 2A, at step 204, three integer values(corresponding to head, size, and tag, respectively) are atomically readfrom a variable H into local variables h, s, and tag, respectively. Atstep 208, it is then determined whether or not the value of s is equalto the capacity of the queue (i.e., the size M of the array W).

If so (i.e., the value of S is equal to M), then at step 212, anindicator is provided that the queue is full. In such a case, the ownerof the queue may be permitted to decide the next course of actionincluding, but not limited to, extending the size of the array W.

If the value of S is not equal to M (i.e., it is smaller than M), thenat step 216, the queue owner writes the item w into the entry of array Wwith index h+s % M. This write operation does not have to be atomic.

At step 220, the queue owner atomically writes to the variable H thethree values h, s+1, tag. That is, the head index is unchanged, but thesize of the queue has increased by one.

At step 224, the Put(w) operation returns a success indicator.

With respect to the Take( ) operation, initially, the Take( ) operationwill be generally described, following by a description of the method230 of FIG. 2B. Only the owner thread (i.e., owner) of the queue canperform a Take( ) operation on the queue. The Take( ) operation returnsa work item that was put earlier by the owner thread, or an indictor ofan empty queue if the queue is empty. The Take( ) operation extracts awork item from the tail end of the queue, i.e., the most recent item putin the queue by the owner.

Referring to FIG. 2B, at step 234, three integer values (head, size,tag) are atomically read from variable H into local variables h, s, andtag, respectively. At step 238 it is determined whether or not s isequal to zero.

If so (i.e., the value of S is equal to zero), then at step 242, anindicator is provided that the queue is empty.

If the value of S is not equal to zero (i.e., it is greater than zero),then at step 246, the queue owner reads the entry of array W with indexh+s−1% M (i.e., the most recently added item). This read operation doesnot have to be atomic.

At step 250, the owner writes atomically to the variable H the threevalues h s−1, and tag+1. That is, the head index is unchanged, but thesize of the queue has decreased by one, and the extraction tag isincremented.

At step 254, the Take( ) operation returns the extracted item.

With respect to the Steal( ) operation, initially, the Steal( )operation will be generally described, following by a description of themethod 260 of FIG. 2C. A Steal( ) operation is executed by a threaddifferent (hereinafter referred to as “other thread” or “otherthread(s)”) from the thread owner. Typically, the other thread has anempty work queue, and hence is looking to help other threads with theirwork. The Steal( ) operation returns a work item that was put in thequeue by the queue's owner, an indicator of an empty queue if the queueis empty, or an indicator of conflict. The Steal( ) operation extracts awork item from the head of the queue, i.e., the oldest item in thequeue.

Referring to FIG. 2C, at step 264, three integer values (correspondingto head, size, tag) are atomically read from a variable H into localvariables h, s, and tag, respectively. At step 268, it is determinedwhether or not s is equal to zero.

If so (i.e., the value of S is equal to zero), then at step 272, anindicator is provided that the queue is empty.

If the value of S is not equal to zero (i.e., it is greater than zero),then at step 276, the other thread reads the entry of array W with indexh, i.e., at the head of the queue. This read need not be atomic.

At step 280, the other thread atomically checks that the value of H isthe same as that read in the first step (i.e., step 264).

If not (i.e., the value of H is different), then at step 292, anindicator is provided that a conflict exists. In such a case, the otherthread may be permitted to decide on the next course of actionincluding, but not limited to, for example, retrying the operation onthis work queue or trying a different work queue.

If the value of H is the same (between steps 264 and 280), then at step284, the other thread writes to H the three values h+1% M, s−1, tag+1.The read-check-write are all done atomically using complex atomicinstructions such as, but not limited to, for example, compare-and-swap.

At step 288, the Steal( ) operation returns the extracted item.

In an embodiment, the methods 200, 230, and 260 of FIGS. 2A, 2B, and 2C,respectively, may be represented by the following pseudo code. In thefollowing pseudo code, the operations Put(w) and Take( ) are performedby the owner only.

Structures

H: <integer,integer,integer>//<Head,Size,Tag>

W: array of tasks of size M

Initialization

H:=<0, 0, 0>

Put (w)

1 <h, s, tag>:=H

-   -   if (s==M) return FULL

2 W [h+s % M]:=w

3 H:=<h, s+1, tag>

-   -   return SUCCESS

Take( )

1 <h, s, tag>:=H

-   -   if (s==0) return EMPTY

2 w:=[h+s−1% M]

3 H:=<h, s−1, tag+1>

-   -   return w

Steal( )

1 <h, s, tag>:=H

-   -   if (s==0) return EMPTY

2 w:=[h % M]

3 if !CAS (H, <h, s, tag>, <h+1% M, s−1, tag+1>) return CONFLICT

-   -   return w

It should be understood that the elements shown in the FIGURES may beimplemented in various forms of hardware, software or combinationsthereof. Preferably, these elements are implemented in software on oneor more appropriately programmed general-purpose digital computershaving a processor and memory and input/output interfaces.

Embodiments of the present invention can take the form of an entirelyhardware embodiment, an entirely software embodiment or an embodimentincluding both hardware and software elements. In a preferredembodiment, the present invention is implemented in software, whichincludes but is not limited to firmware, resident software, microcode,etc.

Furthermore, the invention can take the form of a computer programproduct accessible from a computer-usable or computer-readable mediumproviding program code for use by or in connection with a computer orany instruction execution system. For the purposes of this description,a computer-usable or computer readable medium can be any apparatus thatmay include, store, communicate, propagate, or transport the program foruse by or in connection with the instruction execution system,apparatus, or device. The medium can be an electronic, magnetic,optical, electromagnetic, infrared, or semiconductor system (orapparatus or device) or a propagation medium. Examples of acomputer-readable medium include a semiconductor or solid state memory,magnetic tape, a removable computer diskette, a random access memory(RAM), a read-only memory (ROM), a rigid magnetic disk and an opticaldisk. Current examples of optical disks include compact disk-read onlymemory (CD-ROM), compact disk-read/write (CD-R/W) and DVD.

A data processing system suitable for storing and/or executing programcode may include at least one processor coupled directly or indirectlyto memory elements through a system bus. The memory elements can includelocal memory employed during actual execution of the program code, bulkstorage, and cache memories which provide temporary storage of at leastsome program code to reduce the number of times code is retrieved frombulk storage during execution. Input/output or I/O devices (includingbut not limited to keyboards, displays, pointing devices, etc.) may becoupled to the system either directly or through intervening I/Ocontrollers.

Network adapters may also be coupled to the system to enable the dataprocessing system to become coupled to other data processing systems orremote printers or storage devices through intervening private or publicnetworks. Modems, cable modem and Ethernet cards are just a few of thecurrently available types of network adapters.

Reference in the specification to “one embodiment” or “an embodiment” ofthe present principles, as well as other variations thereof, means thata particular feature, structure, characteristic, and so forth describedin connection with the embodiment is included in at least one embodimentof the present principles. Thus, the appearances of the phrase “in oneembodiment” or “in an embodiment”, as well any other variations,appearing in various places throughout the specification are notnecessarily all referring to the same embodiment.

It is to be appreciated that the use of any of the following “/”,“and/or”, and “at least one of”, for example, in the cases of “A/B”, “Aand/or B” and “at least one of A and B”, is intended to encompass theselection of the first listed option (A) only, or the selection of thesecond listed option (B) only, or the selection of both options (A andB). As a further example, in the cases of “A, B, and/or C” and “at leastone of A, B, and C”, such phrasing is intended to encompass theselection of the first listed option (A) only, or the selection of thesecond listed option (B) only, or the selection of the third listedoption (C) only, or the selection of the first and the second listedoptions (A and B) only, or the selection of the first and third listedoptions (A and C) only, or the selection of the second and third listedoptions (B and C) only, or the selection of all three options (A and Band C). This may be extended, as readily apparent by one of ordinaryskill in this and related arts, for as many items listed.

As used herein, the word “owner” and the phrases “thread owner”, “ownerthread”, and variations thereof, each interchangeably refer to a threadthat currently has ownership (e.g., a lock) on a particular queue. Theparticular queue may be referred to as the “owned queue” and variationsthereof.

Moreover, as used herein, the phrase “atomic operation” refers to a setof operations that can be combined so that they appear to be (to acorresponding system in which they are used) single operation. Examplesof atomic instructions include a read only, a write only, and so forth,where each of these instructions involves only one access.

Further, as used herein, the phrases “complex atomic instruction” and“special atomic instruction” interchangeably refer to atomicinstructions that necessarily involve more than one access. Examples ofcomplex atomic instructions include a read-modify-write, aread-check-write, and so forth. The read and the write are accesses toshared memory, while the check and modify are applied privately to theread value.

FIG. 3 shows an exemplary work queue 300 with two tasks, to which thepresent principles may be applied, in accordance with an embodiment ofthe present principles.

The reference character W represents a circular array of work items ofsize M. With respect to array W, the queue owner puts work items intothe array, and the owner and other threads may take work items from thearray. During normal queue operations (put, take, and steal), the sizeof the array is treated as constant. However, the owner of the queue canresize the array in a straightforward manner as described herein.

The reference character H denotes a single integer variable indicatingthe head of the queue. The initial value of H is zero.

The reference character T denotes a single integer variable indicatingthe tail of the queue. The initial value of T is zero.

FIGS. 4A, 4B, and 4C respectively show exemplary methods 400, 430, and460 relating to double-ended extraction on a work queue, in accordancewith an embodiment of the present principles. In further detail, themethod 400 of FIG. 4A corresponds to a Put(w) operation, the method 430of FIG. 4B corresponds to a Take( ) operation, and the method 460 ofFIG. 4C corresponds to a Steal( ) operation, each corresponding todouble-ended extraction on a work queue. The methods 400, 430, and 460may be applied, for example, to the work queue 300 of FIG. 3.

Initially, the Put(w) operation will be generally described, followingby a description of the method 400 of FIG. 4A. Only the owner thread(i.e., owner) of the queue can perform a Put(w) operation on the queue.The owner puts a new work item at the tail end of the work queue. ThePut(w) operation takes as a parameter the work item to be added to thequeue.

Referring to method 400 of FIG. 4A, at step 404, the owner atomicallyreads the value from T into local variable t.

At step 408, the owner atomically reads the value from H into localvariable h.

At step 412, it is determined if t-h is equal to M.

If so (i.e., if t-h is equal to M), then at step 416, an indicator isprovided that the queue is full.

If t-h is not equal to M, then at step 420, the owner writes the item winto the entry of array W with index t % M. This write operation doesnot have to be atomic.

At step 424, the owner atomically writes the value t+1 to the sharedvariable T.

At step 428, the Put(w) operation returns a success indicator.

Initially, the Take( ) operation will be generally described, followingby a description of the method 230 of FIG. 2B. Only the owner thread(i.e., owner) of the queue can perform a Take( ) operation on the queue.The Take( ) operation returns a work item that was put earlier by theowner thread, or an indictor of an empty queue if the queue is empty.The Take( ) operation extracts a work item from the tail end of thequeue, i.e., the most recent item put in the queue by the owner.

Referring to FIG. 4B, at step 432, the value of T is atomically read,and the value of T minus 1 is kept in a local variable t.

At step 434, the owner atomically writes the value of local variable tinto the shared variable T.

At step 436, the owner atomically reads the value from H into a localvariable h.

At step 438, it is determined whether or not t is smaller than h (i.e.,the queue is empty).

If so (i.e., t is smaller than h), then at step 440, the owner writesthe value h into T. At step 442, the Take( ) operation returns an emptyqueue indicator.

If t is not smaller than h, then at step 444, it is determined whetheror not t is equal to h.

If so (i.e., t is equal to h), then at step 446, the owner atomicallywrites the value h+1 into T. At step 448, the owner atomically writesh+1 into H.

If t is not equal to h (and also following step 448), the Take( )operation returns the item with index t%M in the array W.

With respect to the Steal( ) operation, initially, the Steal( )operation will be generally described, following by a description of themethod 460 of FIG. 4C. A Steal( ) operation is executed by a threaddifferent (hereinafter referred to as “other thread” or “otherthread(s)”) from the thread owner. Typically, the other thread has anempty work queue, and hence is looking to help other threads with theirwork. The Steal( ) operation returns a work item that was put in thequeue by the queue's owner, an indicator of an empty queue if the queueis empty, or an indicator of conflict. The Steal( ) operation extracts awork item from the head of the queue, i.e., the oldest item in thequeue.

Referring to FIG. 4C, at step 462, the Steal( ) operation atomicallyreads from variable H into local variable h.

At step 464, the other thread(s) atomically reads from variable T intolocal variable t.

At step 466, it is determined whether or not h is greater than or equalto t.

If so (h is greater than or equal to t), then at step 468, an indicatoris provided of an empty queue.

If h is not greater than or equal to t, then at step 470, the otherthread(s) reads the entry of array W with index h, i.e., at the head ofthe queue. This read operation does not have to be atomic.

At step 472, it is determined whether or not the value H is the same asthat read in the first step (i.e., step 462). The read-check-write insteps 472 and 476 are complex atomic instructions.

If not (i.e., the value of H is different), then at step 474, anindicator is provided that a conflict exists. In such a case, the otherthread may be permitted to decide on the next course of actionincluding, bit not limited to, retrying the operation on this work queueor trying a different work queue.

If the value of H is the same (between steps 462 and 472), then at step476, the other thread(s) atomically writes the value h+1 to H.

At step 480, the Steal( ) operation returns the extracted item.

In an embodiment, the methods 400, 430, and 460 of FIGS. 4A, 4B, and 4C,respectively, may be represented by the following pseudo code. In thefollowing pseudo code, the operations Put(w) and Take( ) are performedby the owner only.

Structures

H: integer//Head

T: integer//Tail

W: array of tasks of size M

Initialization

H:=0

T:=0

Put(w)

1 t:=T

2 h:=H

-   -   if (t−h==M) return FULL

3 W[t % M]:=w

4 T:=t+1

-   -   return SUCCESS

Take( )

1 t:=T−1

2 T:=t

3 h:=H

4 if (t<h) T:=h; return EMPTY

-   -   if (t==h)

5 T:=h+1

6 H:=h+1

7 return W[t % M]

Steal( )

1 h:=H

2 t:=T

-   -   if (h>=t) return EMPTY

3 w:=W [h % M]

4 if !CAS(H,h,h+1) return CONFLICT

-   -   return w

FIGS. 5A and 5B respectively show exemplary methods 530 and 560 relatingto first in first out (FIFO) extraction on a work queue, in accordancewith an embodiment of the present principles. In further detail, themethod 530 of FIG. 5A corresponds to a Take( ) operation and the method560 of FIG. 5B corresponds to a Steal( ) operation, each correspondingto first in first out (FIFO) extraction on a work queue. The methods 530and 560 may be applied, for example, to the work queue 300 of FIG. 3. Itis to be noted that the Put(w) operation relating to first in first outextraction on a work queue is the same as that described for the Put(w)operation of FIG. 4A (and is, hence, not reproduced again with respectto FIFO extraction for reasons of brevity).

Initially, the Take( ) operation will be generally described, followingby a description of the method 530 of FIG. 5A. Only the owner thread(i.e., owner) of the queue can perform a Take( ) operation on the queue.The Take( ) operation returns a work item that was put earlier by theowner thread, or an indictor of an empty queue if the queue is empty.The Take( ) operation extracts a work item from the head end of thequeue, i.e., the oldest item put in the queue by the owner.

Referring to FIG. 5A, at step 534, the Take( ) operations atomicallyreads the value of H into a local variable h.

At step 538, the owner atomically reads the value from T into a localvariable t.

At step 542, it is determined whether or not h is equal to t.

If so (i.e., h is equal to t), then at step 546, an indicator isprovided that the queue is empty.

If h is not equal to t, then at step 550, the owner reads the entry ofarray W with index h % M, i.e., the oldest item in the queue. This readoperation does not have to be atomic.

At step 554, the owner atomically writes the value h+1 into H.

At step 558, the Take( ) operation returns the extracted item.

With respect to the Steal( ) operation, initially, the Steal( )operation will be generally described, following by a description of themethod 560 of FIG. 5B. A Steal( ) operation is executed by a threaddifferent (hereinafter referred to as “other thread” or “otherthread(s)”) from the thread owner. Typically, the other thread has anempty work queue, and hence is looking to help other threads with theirwork. The Steal( ) operation returns a work item that was put in thequeue by the queue's owner, an indicator of an empty queue if the queueis empty, or an indicator of conflict. The Steal( ) operation extracts awork item from the head of the queue, i.e., the oldest item in thequeue.

Referring to FIG. 5B, at step 564, the Steal( ) operation atomicallyreads from variable H into local variable h.

At step 568, the other thread(s) atomically reads from the variable Tinto a local variable t.

At step 572, it is determined whether or not h is equal to t.

If so (i.e., h is equal to t), then at step 576, an indicator isprovided of an empty queue.

If h is not equal to t, then at step 580, the other thread(s) reads theentry of array W with index h % M, i.e., at the head of the queue. Thisread operation does not have to be atomic.

At step 584, it is determined whether or not the value of H is the sameas that read in the first step (i.e., step 564).

If not (i.e., the value of H is different), then at step 588, anindicator is provided that a conflict exists. In such a case, the otherthread may be permitted to decide on the next course of actionincluding, but not limited to, retrying the operation on this work queueor trying a different work queue.

If the value of H is the same (between steps 564 and 584, then at step592, the other thread(s) atomically writes the value h+1 into H. It isto be noted that read-check-write in steps 592 and 596 are complexatomic instructions.

At step 596, the Steal( ) operation returns the extracted item.

In an embodiment, the method 530 and 560 of FIGS. 5A and 5B and 5C,respectively, may be represented by the following pseudo code. In thefollowing pseudo code, the operations Put(w) and Take( ) are performedby the owner only. It is to be that noted pseudo code for a Put(w)operation corresponding to first out (FIFO) extraction on a work queuemay be represented by the pseudo code provided above with respect to themethod 400 of FIG. 4A.

Structures

H: integer//Head

T: integer//Tail

W: array of tasks of size M

Initialization

H:=0

T:=0

Take( )

1 h:=H

2 t:=T

-   -   if (h==t) return EMPTY

3 w:=W[h % M]

4 H:=h+1

-   -   return w

Steal( )

1 h:=H

2 t:=T

-   -   if (h==t) return EMPTY

3 w:=[h % M]

4 if !CAS(H,h,h+1) return CONFLICT

return w

FIG. 6 shows an exemplary work queue 600 with two tasks, to which thepresent principles may be applied, in accordance with an embodiment ofthe present principles.

The reference character W represents a circular array of work items ofsize M. With respect to array W, the queue owner puts work items intothe array, and the owner and other threads may take work items from thearray. During normal queue operations (put, take, and steal), the sizeof the array is treated as constant. However, the owner of the queue canresize the array in a straightforward manner as described herein.

The reference character T denotes a single variable that can be accessedatomically. T includes two integer components corresponding to the tailof the work queue 600 and tag for the work queue 600, respectively. Thetail of the work queue corresponds to the index of the tail end of thework queue. The tag for the work queue is a number that is incrementedon every extraction. Preferably, the size of the tag is large enough(e.g., 40 bits) such that it is impossible for the tag to make acomplete wrap-around during a single operation on the queue by a thread.The initial value of T is all zeros.

FIGS. 7A, 7B, and 7C respectively show exemplary methods 700, 730, and760 relating to last in first out (LIFO) extraction on a work queue, inaccordance with an embodiment of the present principles. In furtherdetail, the method 700 of FIG. 7A corresponds to a Put(w) operation, themethod 730 of FIG. 7B corresponds to a Take( ) operation, and the method760 of FIG. 7C corresponds to a Steal( ) operation, each correspondingto last in first out (LIFO) extraction on a work queue. The methods 700,730, and 760 may be applied, for example, to the work queue 600 of FIG.6.

Initially, the Put(w) operation will be generally described, followingby a description of the method 700 of FIG. 7A. Only the owner thread(i.e., owner) of the queue can perform a Put(w) operation on the queue.The owner puts a new work item at the tail end of the work queue. ThePut(w) operation takes as a parameter the work item to be added to thequeue.

Referring to method 700 of FIG. 7A, at step 704, the Put(w) operationatomically reads two integer values (corresponding to tail and tag,respectively) are atomically read from variable T into local variables tand tag.

At step 708, it is determined whether or not t is equal to the capacityof the queue (i.e., the size M of the array W).

If so (i.e., the value of t is equal to M), then at step 712, anindicator is provided that the queue is full. In such a case, the ownerof the queue may be permitted to decide the next course of actionincluding, but not limited to, extending the size of the array W.

If the value of t is not equal to M (e.g., it is smaller than M), thenat step 716, the owner writes the item w into the entry of array W withindex t. This write operation does not have to be atomic.

At step 720, the queue owner atomically writes to the variable T the twovalues t+1 and tag.

At step 724, the Put(w) operation returns a success indicator.

With respect to the Take( ) operation, initially, the Take( ) operationwill be generally described, following by a description of the method730 of FIG. 7B. Only the owner thread (i.e., owner) of the queue canperform a Take( ) operation on the queue. The Take( ) operation returnsa work item that was put earlier by the owner thread, or an indictor ofan empty queue if the queue is empty. The Take( ) operation extracts awork item from the tail end of the queue, i.e., the most recent item putin the queue by the owner.

Referring to FIG. 7B, at step 734, the Take( ) operations atomicallyreads two integer values (corresponding to tail and tag, respectively)from variable T into local variables t and tag.

At step 738, it is determined whether or not t is equal to zero.

If so (i.e., the value of t is equal to zero), then at step 742, anindicator is provided that the queue is empty.

If the value of t is not equal to zero, then at step 746, the ownerreads the entry of array W with index t−1, i.e., the most recently addeditem. This read operation does not have to be atomic.

At step 750, the owner atomically writes to the variable T the twovalues t−1 and tag+1.

At step 754, the Take( ) operation returns the extracted item.

With respect to the Steal( ) operation, initially, the Steal( )operation will be generally described, following by a description of themethod 760 of FIG. 7C. A Steal( ) operation is executed by a threaddifferent (hereinafter referred to as “other thread” or “otherthread(s)”) from the thread owner. Typically, the other thread has anempty work queue, and hence is looking to help other threads with theirwork. The Steal( ) operation returns a work item that was put in thequeue by the queue's owner, an indicator of an empty queue if the queueis empty, or an indicator of conflict. The Steal( ) operation extracts awork item from the tail of the queue, i.e., the most recent item in thequeue.

Referring to FIG. 7C, at step 764, the Steal( ) operation atomicallyreads two integer values (corresponding to tail and tag, respectively)from variable T into local variables t and tag.

At step 768, it is determined whether or not t is equal to zero.

If so (i.e., if t is equal to zero), then at step 772, an indicator isprovided that the queue is empty.

If the value of t is not equal to zero, then at step 776, the otherthread(s) read the entry of array W with index t−1. This read operationdoes not have to be atomic.

At step 780, it is determined whether or not the value of t is the sameas that read in the first step (i.e., step 764).

If not (i.e., the value of t is different), then at step 784, anindicator is provided that a conflict exists. In such a case, the otherthread may be permitted to decide on the next course of actionincluding, but not limited to, retrying the operation on this work queueor trying a different work queue.

If the value of t is the same (between steps 764 and 780), then at step788, the other thread atomically writes to the variable H the two valuest−1 and tag+1. It is to be noted that the read-check-write steps of 780and 788 are complex atomic instructions.

At step 792, the Steal( ) operation returns the extracted item.

In an embodiment, the methods 700, 730, and 760 of FIGS. 7A, 7B, and 7C,respectively, may be represented by the following pseudo code. In thefollowing pseudo code, the operations Put(w) and Take( ) are performedby the owner only.

Structures

T: <integer,integer>//<Tail,Tag>

W: array of tasks of size M

Initialization

T:=<0, 0>

Put (w)

1 t, tag:=T

-   -   if (t==M) return FULL

2 W[t]:=w

3 T:=<t+1,tag>

-   -   return SUCCESS

Take( )

1 <t,tag>:=T

-   -   if (t==0) return EMPTY

2 w:=W [t−1]

3 T:=<t−1,tag+1>

-   -   return w

Steal( )

1 <t,tag>:=T

-   -   if (t==0) return EMPTY

2 w:=W[t−1]

3 if !CAS(T,<t,tag>,<t−1,tag+1>) return CONFLICT

-   -   return w

One or more extensions of the present principles, in accordance with oneor more embodiments thereof will now be described. For example, in anembodiment, any of the queues described herein can be grown unbounded.The owner can simply replace the circular array with another circulararray with a different size after copying the items in the old array tothe corresponding locations (modulo array sizes) in the new array. Insystems with automatic garbage collection, the old array is reclaimedautomatically. In systems with explicit memory de-allocation, using anyof the known safe memory reclamation methods, such as hazard pointers,can be used to reclaim the old array.

Having described preferred embodiments of methods (which are intended tobe illustrative and not limiting), it is noted that modifications andvariations can be made by persons skilled in the art in light of theabove teachings. It is therefore to be understood that changes may bemade in the particular embodiments disclosed which are within the scopeand spirit of the invention as outlined by the appended claims. Havingthus described aspects of the invention, with the details andparticularity required by the patent laws, what is claimed and desiredprotected by Letters Patent is set forth in the appended claims.

What is claimed is:
 1. A method, comprising: permitting a single ownerthread of a single owner, multi-consumer, work queue to access the workqueue using atomic instructions limited to only a single access andusing non-atomic operations; restricting the single owner thread fromaccessing the work queue using atomic instructions involving more thanone access; and synchronizing amongst other threads with respect totheir respective accesses to the work queue, wherein the atomicinstructions limited to only a single access consist of only one of asingle read or a single write.
 2. The method of claim 1, furthercomprising prohibiting the single owner thread from re-writing, to thework queue, shared, non-initialization-related variables that havealready been written by the other threads to the work queue.
 3. Themethod of claim 1, further comprising prohibiting the other threads fromre-writing, to the work queue, shared variables that have already beenwritten by the single owner thread to the work queue.
 4. The method ofclaim 1, wherein operations performed by the single owner threadcomprise an insertion operation that involves atomically reading atleast two integer values from a single shared variable having at leasttwo integer components respectively relating to a head of the work queueand a number of items in the work queue into a first and a second localvariable respectively, writing an item into a work queue entry having anindex based on a sum of the first and the second local variables, andatomically writing at least two values to the single shared variable,the at least two values respectively comprising the first local variableand a sum of the second local variable plus one.
 5. The method of claim1, wherein operations performed by the single owner thread comprise anextraction operation that involves atomically reading at least twointeger values from a single shared variable having at least two integercomponents respectively relating to a head of the work queue and anumber of items in the work queue into a first and a second localvariable respectively, reading a work queue entry having an index basedon a sum of the first local variable and the second local variable minusone, and atomically writing at least two values to the single sharedvariable, and returning the work queue entry, the at least two valuesrespectively comprising the first local variable and the second localvariable minus one.
 6. The method of claim 1, wherein an operationperformed by at least one of the other threads comprises atomicallyreading at least two integer values from a single shared variable havingat least two integer components respectively relating to a head of thework queue and a number of items in the work queue into a first and asecond local variable respectively, reading a work queue entry having anindex equal to the first local variable, checking that the value of thesingle shared variable is currently equal to an initially read valuethere for during the operation, writing a sum of the first localvariable plus one and a difference of the second local variable minusone to the single shared variable and providing an indication of successand returning the work queue entry when the value of the single sharedvariable is currently equal to the initially read value there for,wherein the checking and the writing are performed together atomically.7. The method of claim 1, wherein operations performed by the singleowner thread comprise an insertion operation that involves atomicallyreading an integer value from a single shared variable having a singleinteger component relating to a tail of the work queue into a localvariable, writing an item into a work queue entry having an index basedon the local variable, and atomically writing a sum of the localvariable plus one to the single shared variable.
 8. The method of claim1, wherein operations performed by the single owner thread comprise anextraction operation that involves atomically reading an integer valuefrom a single shared variable having a single integer component relatingto a tail of the work queue and maintaining the integer value minus onein a local variable, atomically writing the local variable into thesingle shared variable, atomically reading another integer value fromanother single shared variable having another single integer componentrelating to a head of the work queue into another local variable,checking whether the local variable is less than the other localvariable, writing the other local variable to the single shared variablewhen the local variable is less than the other local variable, andatomically writing a sum of the other local variable plus one into thesingle shared variable and the other single shared variable andreturning a work queue item having an index equal to the local variablewhen the local variable is not less than the other local variable. 9.The method of claim 1, wherein an operation performed by at least one ofthe other threads comprises atomically reading at least one integervalue from a single shared variable having at least one integercomponent relating to a head of the work queue into a local variable,reading a work queue entry having an index equal to the local variable,checking that the value of the single shared variable is currently equalto an initially read value there for during the operation, writing a sumof the local variable plus one to the single shared variable andproviding an indication of success and returning the work queue entrywhen the value of the single shared variable is currently equal to theinitially read value there for, wherein the checking and the writing areperformed together atomically.
 10. The method of claim 1, wherein thework queue is configured as a circular array.
 11. A non-transitoryprogram storage device readable by machine, tangibly embodying a programof instructions executable by the machine to perform method steps forqueue access management, the method steps comprising: permitting asingle owner thread of a single owner, multi-consumer, work queue toaccess the work queue using atomic instructions limited to only a singleaccess and using non-atomic operations; restricting the single ownerthread from accessing the work queue using atomic instructions involvingmore than one access; and synchronizing amongst other threads withrespect to their respective accesses to the work queue, wherein theatomic instructions limited to only a single access consist of only oneof a single read or a single write.
 12. The program storage device ofclaim 11, further comprising prohibiting the single owner thread fromre-writing, to the work queue, shared, non-initialization-relatedvariables that have already been written by the other threads to thework queue.
 13. The program storage device of claim 11, furthercomprising prohibiting the other threads from re-writing, to the workqueue, shared variables that have already been written by the singleowner thread to the work queue.
 14. The program storage device of claim11, wherein operations performed by the single owner thread comprise aninsertion operation that involves atomically reading at least twointeger values from a single shared variable having at least two integercomponents respectively relating to a head of the work queue and anumber of items in the work queue into a first and a second localvariable respectively, writing an item into a work queue entry having anindex based on a sum of the first and the second local variables, andatomically writing at least two values to the single shared variable,the at least two values respectively comprising the first local variableand a sum of the second local variable plus one.
 15. The program storagedevice of claim 11, wherein operations performed by the single ownerthread comprise an extraction operation that involves atomically readingat least two integer values from a single shared variable having atleast two integer components respectively relating to a head of the workqueue and a number of items in the work queue into a first and a secondlocal variable respectively, reading a work queue entry having an indexbased on a sum of the first local variable and the second local variableminus one, and atomically writing at least two values to the singleshared variable, and returning the work queue entry, the at least twovalues respectively comprising the first local variable and the secondlocal variable minus one.
 16. The program storage device of claim 11,wherein an operation performed by at least one of the other threadscomprises atomically reading at least two integer values from a singleshared variable having at least two integer components respectivelyrelating to a head of the work queue and a number of items in the workqueue into a first and a second local variable respectively, reading awork queue entry having an index equal to the first local variable,checking that the value of the single shared variable is currently equalto an initially read value there for during the operation, writing a sumof the first local variable plus one and a difference of the secondlocal variable minus one to the single shared variable and providing anindication of success and returning the work queue entry when the valueof the single shared variable is currently equal to the initially readvalue there for, wherein the checking and the writing are performedtogether atomically.
 17. The program storage device of claim 11, whereinoperations performed by the single owner thread comprise an insertionoperation that involves atomically reading an integer value from asingle shared variable having a single integer component relating to atail of the work queue into a local variable, writing an item into awork queue entry having an index based on the local variable, andatomically writing a sum of the local variable plus one to the singleshared variable.
 18. The program storage device of claim 11, whereinoperations performed by the single owner thread comprise an extractionoperation that involves atomically reading an integer value from asingle shared variable having a single integer component relating to atail of the work queue and maintaining the integer value minus one in alocal variable, atomically writing the local variable into the singleshared variable, atomically reading another integer value from anothersingle shared variable having another single integer component relatingto a head of the work queue into another local variable, checkingwhether the local variable is less than the other local variable,writing the other local variable to the single shared variable when thelocal variable is less than the other local variable, and atomicallywriting a sum of the other local variable plus one into the singleshared variable and the other single shared variable and returning awork queue item having an index equal to the local variable when thelocal variable is not less than the other local variable.
 19. Theprogram storage device of claim 11, wherein an operation performed by atleast one of the other threads comprises atomically reading at least oneinteger value from a single shared variable having at least one integercomponent relating to a head of the work queue into a local variable,reading a work queue entry having an index equal to the local variable,checking that the value of the single shared variable is currently equalto an initially read value there for during the operation, writing a sumof the local variable plus one to the single shared variable andproviding an indication of success and returning the work queue entrywhen the value of the single shared variable is currently equal to theinitially read value there for, wherein the checking and the writing areperformed together atomically.
 20. The program storage device of claim11, wherein the work queue is configured as a circular array.